package cn.sfa.common.utils;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

public class FilterSpesChar {

	 //匹配图片内容
	 public static String matchImage(String str) {
	  List<String> imgList = new ArrayList<String>();
	  String regEx = "<img.*?src=\"(.*?)\"";
	  Pattern pat = Pattern.compile(regEx);
	  Matcher mat = pat.matcher(str);
	  while (mat.find()) {
		   
	   imgList.add(mat.group(1));
	  }
	  
	  if(imgList.size()>0)
		  return (String)imgList.get(0);
	  return "";
	 }
	 
	 //去除HTML标签
	 public static String HtmlText(String inputString) { 
	      String htmlStr = inputString; //含html标签的字符串 
	      String textStr =""; 
	      java.util.regex.Pattern p_script; 
	      java.util.regex.Matcher m_script; 
	      java.util.regex.Pattern p_style; 
	      java.util.regex.Matcher m_style; 
	      java.util.regex.Pattern p_html; 
	      java.util.regex.Matcher m_html; 
	      try { 
	       String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; //定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script> } 
	       String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; //定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style> } 
	          String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式 
	      
	          p_script = Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE); 
	          m_script = p_script.matcher(htmlStr); 
	          htmlStr = m_script.replaceAll(""); //过滤script标签 
	
	          p_style = Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE); 
	          m_style = p_style.matcher(htmlStr); 
	          htmlStr = m_style.replaceAll(""); //过滤style标签 
	      
	          p_html = Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE); 
	          m_html = p_html.matcher(htmlStr); 
	          htmlStr = m_html.replaceAll(""); //过滤html标签 
	          
	          /* 空格 —— (包括全角空格，制表符，回车等)  */
//	         p_html = Pattern.compile("[\\s\\p{Zs}]", Pattern.CASE_INSENSITIVE);
	          p_html = Pattern.compile("&nbsp;", Pattern.CASE_INSENSITIVE);
	          m_html = p_html.matcher(htmlStr);
	          htmlStr = m_html.replaceAll("");

	      
	       textStr = htmlStr; 
	      
	      }catch(Exception e) { 
	      } 
	      return textStr;
   } 
	 
	 
	//是否是正确的图片
	 public static boolean isTrueImage(String str,String basePath)   {
	  
	  String regEx = "<img.*?src=\"(.*?)\"";
	  Pattern pat = Pattern.compile(regEx);
	  Matcher mat = pat.matcher(str);
	  while (mat.find()) {
		  System.out.println("=============="+mat.group(1));
		  try
		  {
			  
		  if(!isImage(mat.group(1),basePath))//如果有图片，则判断图片路径是否正确
		  return false;
		  }catch(java.net.UnknownHostException e)
		  {
			  System.out.println("未知的主机！异常信息："+e.getMessage());
			  return false;
		  }catch(FileNotFoundException e)
		  {
			  System.out.println("图片没有找到！异常信息："+e.getMessage());
			  return false;
		  } catch (Exception e) {
			  System.out.println("出错原因："+e.getMessage());
			return false;
		}
	  }
	  return true;//没有图片直接返回true,不再判断
	  
	 }
	 
	 
	//获取链接地址文件的byte数据  
	    public static byte[] getUrlFileData(String fileUrl) throws Exception  
	    {  
	        URL url = new URL(fileUrl);  
	        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();  
	        httpConn.connect();  
	        InputStream cin = httpConn.getInputStream();  
      ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
	        byte[] buffer = new byte[1024];  
	       int len = 0;  
	       while ((len = cin.read(buffer)) != -1) {  
	            outStream.write(buffer, 0, len);  
	       }  
	        cin.close();  
	        byte[] fileData = outStream.toByteArray();  
	        outStream.close();  
	        return fileData;  
	   } 
	    
	    //是否是图片
	    public static boolean isImage(String srcFileName,String basePath) throws Exception {
	    	if(CommonUtil.isEmpty(srcFileName))
	    	{
	    		return false;
	    	}
	    	
	    	
			  
			  if(!srcFileName.contains(":"))
			  {
				  srcFileName=basePath+srcFileName;
			  }
			  System.out.println("==============pathpath:::"+srcFileName);
	    	
	    	
			  if(srcFileName.contains("&amp;") && srcFileName.contains("?"))//流方式的图片
			  {
	    		srcFileName=srcFileName.replaceAll("&amp;", "&");
			  }else//图片后缀的图片
			  {
			    	String[] picPath=srcFileName.split("\\.");
			    	int length=picPath.length;
			    	
			        if(CommonUtil.isEmpty(picPath) || picPath.length<=1)
			        {
			        	return false;
			        }
			        
			        
			        if(!"gif".equals(picPath[length-1].toLowerCase()) &&
			        		!"jpg".equals(picPath[length-1].toLowerCase())&&
			        		!"png".equals(picPath[length-1].toLowerCase())&&
			        		!"bmp".equals(picPath[length-1].toLowerCase())&&
			        		!"ico".equals(picPath[length-1].toLowerCase())&&
			        		!"tga".equals(picPath[length-1].toLowerCase())&&
			        		!"jpeg".equals(picPath[length-1].toLowerCase())&&
			        		!"PCX".equals(picPath[length-1].toLowerCase())&&
			        		!"TIFF".equals(picPath[length-1].toLowerCase())&&
			        		!"CUR".equals(picPath[length-1].toLowerCase())&&
			        		!"ANI".equals(picPath[length-1].toLowerCase()))
			        {
			        	return false;
			        }
	       
			  }
		    	byte[] b =null;
		    	try
		    	{
		    		b=getUrlFileData(srcFileName);
		    	}catch(Exception e)
		    	{
		    		System.out.println(e.getMessage());
		    		return false;
		    	}
		    	int l = -1;
		    	l = b.length;
		    	String GetImageFileForm="";
		    	if (l > 10) {
		    	byte b0 = b[0];
		    	byte b1 = b[1];
		    	byte b2 = b[2];
		    	byte b3 = b[3];
		    	byte b4 = b[4];
		    	byte b5 = b[5];
		    	byte b6 = b[6];
		    	byte b7 = b[7];
		    	byte b8 = b[8];
		    	byte b9 = b[9];
		    	
		      if ((b0 == 66) && (b1 == 77)) {
		        GetImageFileForm = "BMP";
		        
		      }
		      if ((b0 == 255) && (b1 == 216)) {
		        GetImageFileForm = "JPEG";   
		        
		      }
		      if ((b0 == 71) && (b1 == 73) && (b2 == 70) && (b4 == 57)) {
		        GetImageFileForm = "GIF89";
		        
		      }
		      if ((b0 == 71) && (b1 == 73) && (b2 == 70) && (b4 == 55)) {
		        GetImageFileForm = "GIF87";
		        
		      }
		      if ((b0 == 137) && (b1 == 80)) {
		        GetImageFileForm = "PNG";  
		        
		      }
		      if ((b0 == 73) && (b1 == 73)) {
		        GetImageFileForm = "TifF";//  'TifF 摩托罗拉
		        
		      }
		      if ((b0 == 77) && (b1 == 77)) {
		        GetImageFileForm = "TifF";//  'TifF Intel
		        
		      }
		      if ((b2 == 1) && (b4 == 1)) {
		        GetImageFileForm = "ICO";
		        
		      }
		      if ((b2 == 2) && (b4 == 1)) {
		        GetImageFileForm = "CUR";
		        
		      }
		      if ((b0 == 82) && (b1 == 73) && (b2 == 70) && (b3 == 70)) {
		        GetImageFileForm = "ANI";  
		        
		      }
		      if ((b2 == 2) && (b4 == 0)) {
		        GetImageFileForm = "[TGA Normal]";
		        
		      }
		      if ((b2 == 16) && (b4 == 0)) {
		        GetImageFileForm = "[TGA RLE]";
		        
		      }
		      if (b0 == 10) {
		        GetImageFileForm = "PCX";
		        
		      }
		      
		      if (b0 == (byte) 'G' && b1 == (byte) 'I' && b2 == (byte) 'F') {
		    		System.out.println("Gif文件");
		    		 GetImageFileForm = "GIF";
			    	}  
		      if (b1 == (byte) 'P' && b2 == (byte) 'N' && b3 == (byte) 'G') {
			    		System.out.println("PNG文件");
			    		 GetImageFileForm = "PNG";
			    	}  
		      if (b6 == (byte) 'J' && b7 == (byte) 'F' && b8 == (byte) 'I'&& b9 == (byte) 'F') {
			    		System.out.println("JPG文件");
			    		 GetImageFileForm = "JPG";
			    	}  
		    	
		    	}
		    	
		    	
		     
		      System.out.println(GetImageFileForm);
		      if(CommonUtil.isEmpty(GetImageFileForm))
		      {
		    	  return false;
		      }
		      return true;
		    	
		    	
		    	/*if (b0 == (byte) 'G' && b1 == (byte) 'I' && b2 == (byte) 'F') {
		    		System.out.println("Gif文件");
			    		return true;
			    	} else if (b1 == (byte) 'P' && b2 == (byte) 'N' && b3 == (byte) 'G') {
			    		System.out.println("PNG文件");
			    		return true;
			    	} else if (b6 == (byte) 'J' && b7 == (byte) 'F' && b8 == (byte) 'I'&& b9 == (byte) 'F') {
			    		System.out.println("JPG文件");
			    		return true;
			    	} else {
			    		System.out.println("未知文件类型");
			    		return false;
			    	}
		    	} else {
		    	return false;
		    	}*/
	    	}
	 
	 public static void main(String[] args) throws Exception
	 {
		 System.out.println(isImage("http://oa.hkfs.cn/seeyon/fileUpload.do?method=doDownload&amp;fileId=4971850290044356919&amp;createDate=2012-06-13&amp;filename=img.gif","http://localhost:8080"));
		 String regEx = "&.+;";
		  Pattern pat = Pattern.compile(regEx);
		  Matcher m=pat.matcher("sfs&amp;sfs");
		 System.out.println(m.replaceAll(""));
	 }
}
	

